/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gestionedt.enseignants;

import gestionedt.models.Enseignant;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 * 
 */
public class EnseignantsForm extends org.apache.struts.action.ActionForm {

    private List<Enseignant> enseignants;

    private int idEnseignant;

    private Enseignant enseignant;

    private String[] numsIndispo;

    /**
     *
     */
    public EnseignantsForm() {
        super();
        this.enseignant = new Enseignant();
        this.numsIndispo = new String[0];
    }

    public Enseignant getEnseignant() {
        return enseignant;
    }

    public void setEnseignant(Enseignant enseignant) {
        this.enseignant = enseignant;
    }

    public List<Enseignant> getEnseignants() {
        return enseignants;
    }

    public void setEnseignants(List<Enseignant> enseignants) {
        this.enseignants = enseignants;
    }

    public int getIdEnseignant() {
        return idEnseignant;
    }

    public void setIdEnseignant(int idEnseignant) {
        this.idEnseignant = idEnseignant;
    }

    public String[] getNumsIndispo() {
        return numsIndispo;
    }

    public int[] getNumsIntIndispo() {
        int[] ids = new int[numsIndispo.length];
        int i = 0;
        for(String id : numsIndispo)
            ids[i++] = Integer.parseInt(id);
        return ids;
    }

    public void setNumsIndispo(String[] numsIndispo) {
        this.numsIndispo = numsIndispo;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if(enseignant.getNom() == null || enseignant.getNom().trim().length() < 1)
            errors.add("enseignant.nom", new ActionMessage("errors.required",
                                                           "Le champ nom"));
        if(enseignant.getPrenom() == null || enseignant.getPrenom().trim().length() < 1)
            errors.add("enseignant.prenom", new ActionMessage("errors.required",
                                                              "Le champ prénom"));
        return errors;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        super.reset(mapping, request);

        this.enseignants = new ArrayList<Enseignant>();
        this.idEnseignant = 0;
        this.enseignant = new Enseignant();
        this.numsIndispo = new String[0];
    }
}
